//
//  QSRequestManager.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/23.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSRequestManager.h"
#import "QSRequestHelper.h"

@implementation QSRequestManager
+ (NSURLSessionTask *)executeGET:(NSString *)requestUrl
                     requestBody:(NSMutableDictionary *)bodyDic
                        andBlock:(void (^)(ResponseMessage *, NSDictionary *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:NO];
    
    return [QSRequestHelper executeRequestWithType:QSRequestTypeGet
                                           headers:headers
                                        requestUrl:requestUrl
                                      requestCount:1
                                       requestBody:bodyDic
                                          andBlock:block];
}

+ (NSURLSessionTask *)executePOST:(NSString *)requestUrl
                      requestBody:(NSMutableDictionary *)bodyDic
                         andBlock:(void (^)(ResponseMessage *, NSDictionary *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:NO];

    return [QSRequestHelper executeRequestWithType:QSRequestTypePost
                                           headers:headers
                                        requestUrl:requestUrl
                                      requestCount:1
                                       requestBody:bodyDic
                                          andBlock:block];
}

+ (NSURLSessionTask *)executeTokenPOST:(NSString *)requestUrl
                           requestBody:(NSMutableDictionary *)bodyDic
                              andBlock:(void (^)(ResponseMessage *, NSDictionary *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:YES];

    return [QSRequestHelper executeRequestWithType:QSRequestTypePost
                                           headers:headers
                                        requestUrl:requestUrl
                                      requestCount:1
                                       requestBody:bodyDic
                                          andBlock:block];
}

+ (NSURLSessionTask *)executeJSONTokenPOST:(NSString *)requestUrl
                          requestBody:(NSMutableDictionary *)bodyDic
                             andBlock:(void (^)(ResponseMessage *rspStatusAndMessage, NSDictionary *responseDataDic))block {
    NSMutableDictionary *headers = [self getHeaderDictionary:YES];
    
    return [QSRequestHelper executeRequestWithType:QSRequestTypePost
                                       contentType:QSRequestContentTypeJSON
                                           headers:headers
                                        requestUrl:requestUrl
                                      requestCount:1
                                       requestBody:bodyDic
                                          andBlock:block];
}


+ (NSURLSessionTask *)executeTokenGET:(NSString *)requestUrl
                          requestBody:(NSMutableDictionary *)bodyDic
                             andBlock:(void (^)(ResponseMessage *, NSDictionary *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:YES];

    return [QSRequestHelper executeRequestWithType:QSRequestTypeGet
                                           headers:headers
                                        requestUrl:requestUrl
                                      requestCount:1
                                       requestBody:bodyDic
                                          andBlock:block];
}

+ (NSURLSessionTask *)executeTokenReturnArrayGET:(NSString *)requestUrl
                          requestBody:(NSMutableDictionary *)bodyDic
                             andBlock:(void (^)(ResponseMessage *, NSArray *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:YES];

    return [QSRequestHelper executeRequestReturnArrayWithType:QSRequestTypeGet contentType:QSRequestContentTypeForm headers:headers requestUrl:requestUrl requestCount:1 requestBody:bodyDic andBlock:block];
}

+ (NSURLSessionTask *)excuteTokenUpload:(NSString *)requestUrl
                            requestBody:(NSMutableDictionary *)bodyDic
                                 photos:(NSArray<QSUploadImage *> *)photos
                               andBlock:(void (^)(ResponseMessage *rspStatusAndMessage, NSDictionary *responseDataDic))block {
    return [self excuteTokenUpload:requestUrl
                       requestBody:bodyDic
                            photos:photos
                            videos:nil
                          andBlock:block];
}

+ (NSURLSessionTask *)excuteTokenUpload:(NSString *)requestUrl requestBody:(NSMutableDictionary *)bodyDic
                                 photos:(NSArray<QSUploadImage *> *)photos
                                 videos:(NSArray<QSUploadVideo *> *)videos
                               andBlock:(void (^)(ResponseMessage *, NSDictionary *))block {
    
    NSMutableDictionary *headers = [self getHeaderDictionary:YES];

    if (videos.count) {
        return [QSRequestHelper
                uploadVideoWithPath:requestUrl
                headers:headers
                videos:videos
                params:bodyDic
                andBlock:block];
    }
    
    return [QSRequestHelper uploadImageWithPath:requestUrl
                                        headers:headers
                                         photos:photos
                                         params:bodyDic
                                       andBlock:block];
}


+ (NSMutableDictionary *)getHeaderDictionary:(BOOL)isNeedToken {
    NSString *token = [QSUserManager sharedQSUserManager].user.allToken;
    
    if (!isNeedToken) {
        token = @"";
    }
    /*
     kAppTempLoginChannelId 未登录的时候选择的渠道ID 不保存本地 临时使用
     kAppChannelId 当前登录的渠道ID 保存本地 可一直使用
     */
//    NSString *channelId = kAppTempLoginChannelId.length ? kAppTempLoginChannelId : kAppChannelId;

    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    headers = [@{
        @"allToken": QSNoNilString(token) 
                 } mutableCopy];
  
    
    return headers;
}
@end
